﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Data.SqlClient;
using System.Data;

namespace OgilvyOne.PreorderWap
{
    public class PreorderWapDAL
    {
        private const string sql = "INSERT INTO preorder(name,sex,mobile,email,budget,mention,capcity,carModel,carColor,carSeat,creatTime,way,source,channel) VALUES(@name,@sex,@mobile,@email,@budget,@mention,@capcity,@carModel,@carColor,@carSeat,getDate(),'mobile',@source,@channel);select @@IDENTITY";
        public int InsertWap(string name, string sex, string mobile, string email, string budget, string mention, string capcity, string carModel, string carColor, string carSeat,string source,string channel)
        {
            int id = 0;
            SqlParameter[] parms = new SqlParameter[]{
                new SqlParameter{ParameterName="@name", SqlDbType=SqlDbType.NVarChar,SqlValue=name},
                new SqlParameter{ParameterName="@sex", SqlDbType=SqlDbType.NVarChar,SqlValue=sex},
                new SqlParameter{ParameterName="@mobile", SqlDbType=SqlDbType.NVarChar,SqlValue=mobile},
                new SqlParameter{ParameterName="@email", SqlDbType=SqlDbType.NVarChar,SqlValue=email},
                new SqlParameter{ParameterName="@budget", SqlDbType=SqlDbType.NVarChar,SqlValue=budget},
                new SqlParameter{ParameterName="@mention", SqlDbType=SqlDbType.NVarChar,SqlValue=mention},
                new SqlParameter{ParameterName="@capcity", SqlDbType=SqlDbType.NVarChar,SqlValue=capcity},
                new SqlParameter{ParameterName="@carModel", SqlDbType=SqlDbType.NVarChar,SqlValue=carModel},
                new SqlParameter{ParameterName="@carColor", SqlDbType=SqlDbType.NVarChar,SqlValue=carColor},
                new SqlParameter{ParameterName="@carSeat", SqlDbType=SqlDbType.NVarChar,SqlValue=carSeat},
                new SqlParameter{ParameterName="@source", SqlDbType=SqlDbType.NVarChar,SqlValue=source},
                new SqlParameter{ParameterName="@channel", SqlDbType=SqlDbType.NVarChar,SqlValue=channel}

            };
            for (int i = 0; i < parms.Length; i++)
            {
                if (parms[i].Value == null)
                    parms[i].Value = DBNull.Value;
            }
            string s = SQLHelper.ExecuteSql(sql, parms).ToString();
            id = int.Parse(s);
            return id;
        }


        private const string isEmailSql = "select count(email) from preorder where email=@email";
        public int IsEmail(string email)
        {
            SqlParameter[] parms = new SqlParameter[]{
                new SqlParameter{ParameterName="@email", SqlDbType=SqlDbType.NVarChar,SqlValue=email}
            };
            for (int i = 0; i < parms.Length; i++)
            {
                if (parms[i].Value == null)
                    parms[i].Value = DBNull.Value;
            }
            object obj = SQLHelper.ExecuteScalar(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, isEmailSql, parms);
            return Convert.ToInt32(obj.ToString());
        }
    }
}
